using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace fuzhu
{
    /// <summary>
    /// Excel工作表选择对话框
    /// </summary>
    public partial class SheetSelectionForm : Form
    {
        private ListBox listBoxSheets;
        private Button btnOK;
        private Button btnCancel;
        private Label lblInstruction;
        
        public int SelectedSheetIndex { get; private set; } 
        public string SelectedSheetName { get; private set; }  
        
        public SheetSelectionForm(List<string> sheetNames)
        {
            InitializeComponent();
            LoadSheetNames(sheetNames);
        }
        
        private void InitializeComponent()
        {
            this.Text = "选择工作表";
            this.Size = new Size(350, 280);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ShowIcon = false;
            
            SelectedSheetIndex=-1;
            SelectedSheetName="";
            // 说明标签
            lblInstruction = new Label();
            lblInstruction.Text = "检测到Excel文件包含多个工作表，请选择要导入的工作表：";
            lblInstruction.Location = new Point(15, 15);
            lblInstruction.Size = new Size(310, 20);
            lblInstruction.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular);
            
            // 工作表列表
            listBoxSheets = new ListBox();
            listBoxSheets.Location = new Point(15, 45);
            listBoxSheets.Size = new Size(310, 150);
            listBoxSheets.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular);
            listBoxSheets.SelectionMode = SelectionMode.One;
            listBoxSheets.DoubleClick += ListBoxSheets_DoubleClick;
            
            // 确定按钮
            btnOK = new Button();
            btnOK.Text = "确定";
            btnOK.Location = new Point(170, 210);
            btnOK.Size = new Size(75, 25);
            btnOK.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular);
            btnOK.Click += BtnOK_Click;
            btnOK.Enabled = false;            
            // 取消按钮
            btnCancel = new Button();
            btnCancel.Text = "取消";
            btnCancel.Location = new Point(250, 210);
            btnCancel.Size = new Size(75, 25);
            btnCancel.Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular);
            btnCancel.DialogResult = DialogResult.Cancel;
            btnCancel.Click += BtnCancel_Click;
            
            // 添加控件到窗体
            this.Controls.Add(lblInstruction);
            this.Controls.Add(listBoxSheets);
            this.Controls.Add(btnOK);
            this.Controls.Add(btnCancel);
            
            // 设置默认按钮和取消按钮
            this.AcceptButton = btnOK;
            this.CancelButton = btnCancel;
            
            // 列表选择变化事件
            listBoxSheets.SelectedIndexChanged += ListBoxSheets_SelectedIndexChanged;
        }
        
        private void LoadSheetNames(List<string> sheetNames)
        {
            listBoxSheets.Items.Clear();
            
            for (int i = 0; i < sheetNames.Count; i++)
            {
                string displayText = string.Format("{0}. {1}",i + 1,sheetNames[i]);
                listBoxSheets.Items.Add(displayText);
            }
            
            // 默认选择第一个工作表
            if (listBoxSheets.Items.Count > 0)
            {
                listBoxSheets.SelectedIndex = 0;
            }
        }
        
        private void ListBoxSheets_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnOK.Enabled = listBoxSheets.SelectedIndex >= 0;
        }
        
        private void ListBoxSheets_DoubleClick(object sender, EventArgs e)
        {
            if (listBoxSheets.SelectedIndex >= 0)
            {
                ConfirmSelection();
            }
        }
        
        private void BtnOK_Click(object sender, EventArgs e)
        {
            ConfirmSelection();
        }
        
        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        
        private void ConfirmSelection()
        {
            if (listBoxSheets.SelectedIndex >= 0)
            {
                SelectedSheetIndex = listBoxSheets.SelectedIndex;
                string selectedText = listBoxSheets.SelectedItem.ToString();
                // 提取工作表名称（去掉序号）
                int dotIndex = selectedText.IndexOf(". ");
                if (dotIndex > 0 && dotIndex < selectedText.Length - 2)
                {
                    SelectedSheetName = selectedText.Substring(dotIndex + 2);
                }
                else
                {
                    SelectedSheetName = selectedText;
                }
                
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}